	/*
		1,Esc按钮关闭
		2,暴露 close 方法
		3,alert,confirm(取消返回false,确定返回true),prompt(几秒关闭)
		4,参数:


		bgOpacity:
		callback:
		type:{
			prompt:{
				closeCallback:
				autoClose:true
				closeTime:3//
			}
			alert:{
				closeCallback:
			}
			confirm:{
				confirmButtonCallback:
				cancelButtonCallback:
			}
		}
	 */

$.popupBox = function (options) {
    var _opts = $.extend({}, $.popupBox.defaults, options),//参数
    themeHtml,
    $box;//弹出框元素(是个jQuery对象)
    if (_opts.defaultTheme == "") {
        if (!_opts.customBox) return alert("请输入自定义内容");
        themeHtml = _opts.customBox;
    } else {
        if (_opts.defaultTheme == "tip") {
            themeHtml = '<div id="popupBox" class="information_pop">';
            themeHtml += '<div class="information_layer">';
            themeHtml += '<div class="information_bg">';
            themeHtml += '<div class="pop_main">';
            themeHtml += '<div class="pop_main_mian">';
            themeHtml += '<p class="h25">' + _opts.content + '</p>';
            themeHtml += '</div>';
            themeHtml += '</div>';
            themeHtml += '</div>';
            themeHtml += '</div>';
            themeHtml += '</div>';
        } else if (_opts.defaultTheme == "alert") {
            themeHtml += '<div id="popupBox" class="information_pop">';
            themeHtml += '<div class="information_layer">';
            themeHtml += '<div class="information_bg1">';
            themeHtml += '<div class="pop_main1">';
            themeHtml += '<div class="pop_main1_top">';
            themeHtml += '<span class="fr pt18"><a href="#" data-button="close"><img src="images/icon_188.png">x</a></span>' + _opts.title + '';
            themeHtml += '</div>';
            themeHtml += '<div class="pop_main1_mian pb10">' + _opts.content + '</div>';
            themeHtml += '<div class="pop_main1_bottom">';
            themeHtml += '<a data-button="confirm" href="#" class="pop_a_blue">' + _opts.confirmButton + '</a>';
            themeHtml += '</div>';
            themeHtml += '</div>';
            themeHtml += '</div>';
            themeHtml += '</div>';
            themeHtml += '</div>';
        } else if (_opts.defaultTheme == "confirm") {
            themeHtml = '<div id="popupBox" class="information_pop">';
            themeHtml += '<div class="information_layer">';
            themeHtml += '<div class="information_bg">';
            themeHtml += '<div class="pop_main">';
            themeHtml += '<div class="pop_main_top">';
            themeHtml += '<span class="fr pt10"><a href="#" data-button="close"><img src="images/icon_191.png">x</a></span>' + _opts.title;
            themeHtml += '</div>';
            themeHtml += '<div class="pop_main_mian">';
            themeHtml += '<p class="pop_main_p"></p>';
            themeHtml += '<div class="clrfix"><a data-button="confirm">' + _opts.confirmButton + '</a><a data-button="cancel">' + _opts.cancelButton + '</a></div>';
            themeHtml += '</div>';
            themeHtml += '</div>';
            themeHtml += '</div>';
            themeHtml += '</div>';
            themeHtml += '</div>';
        }
    }
    if (themeHtml.jQuery) {
        $box = themeHtml;
    } else {
        $box = $(themeHtml);
    }
    $box.appendTo("body");
    var setPositionOpts = $.extend({},_opts,{
        elem:$box
    });
    $.popupBox.setPosition(setPositionOpts);
    //按下ESC键关闭弹出框
    $("body").on("keydown", function (e) {
        if (event.keyCode == 27) {
            $.popupBox.close();
        }
    })
    //绑定关闭按钮(如果有的话)
    $("[data-button='close']", "#popupBox").on("click", function () {
        if ($.isFunction(_opts.closeCallback)) {
            if (!_opts.closeCallback()) {
                $.popupBox.close();
            }
        }
    })
    //绑定取消按钮(如果有的话)
    $("[data-button='cancel']", "#popupBox").on("click", function () {
        if ($.isFunction(_opts.cancelCallback)) {
            if (!_opts.cancelCallback()) {
                $.popupBox.close();
            }
        }
    })
    //绑定确定按钮(如果有的话)
    $("[data-button='confirm']", "#popupBox").on("click", function () {
        if ($.isFunction(_opts.confirmCallback)) {
            if (_opts.confirmCallback()) {
                $.popupBox.close();
            }
        }
    })
    //设置自动关闭
    if (_opts.autoClose > 0) {
        setTimeout(function () {
            $.popupBox.close();
        }, _opts.autoClose * 1000)
    }
};
//弹出框关闭事件
$.popupBox.close = function () {
    $("#popupBoxBg,#popupBoxBgIframe").remove();
    $("#popupBox").remove();
};
//注意:所有的弹出框都有关闭按钮
$.popupBox.defaults = {
    autoClose: 0,//自动关闭的时间,以秒为单位,如果0表示不自动关闭
    defaultTheme: "",//是否使用默认主题,为空时表示不使用,默认主题包括"tip","alert","confirm"
    bgOpacity: 0.2,//背景透明度,如果背景透明度为0,表示没有背景
    stick: "window",//弹出框相对的元素
    fixed: true,//位置是否固定在页面上,不跟随滚动条
    top: "center",//距离顶部位置
    left: "center",//距离左边位置
    animate: "",//展示的方式
    showCallback: function () {//弹出框显示后的回调函数
    },
    title: "标题",//标题内容(支持html)
    content: "内容",//内容内容(支持html)
    customBox: "",//自定义弹出框的内容(支持html)
    confirmButton: "确定",//确定按钮文字
    cancelButton: "取消",//取消按钮文字
    closeCallback: function () {//点击关闭按钮的回调函数,如果无返回值或返回值为false,则关闭弹出框
    },
    confirmCallback: function () {//点击关闭按钮的回调函数,如果无返回值或返回值为true,则关闭弹出框
    },
    cancelCallback: function () {//点击关闭按钮的回调函数,如果无返回值或返回值为false,则关闭弹出框
    }
};
//jQuery静态函数
$.popupBox.setPosition = function (opts) {
    var _opts = $.extend({}, $.popupBox.setPosition.defaults, opts);
    _opts.elem.css({
        "position": "absolute",//这里提前定义absolute是因为要获得元素的width和height;否则为其父级元素的width和height
        "zIndex": 1005
    });
    var _top,
    _left,
    $popupBox,
    $popupBoxBg,
    $popupBoxBgIframe,
    $body = $("body"),
    $window = $(window),
    windowWid = $window.width(),
    windowHei = $window.height(),
    bodyWid = $body.width(),
    bodyHei = $body.height(),
    _scrollTop = document.documentElement.scrollTop,
    _scrollLeft = document.documentElement.scrollLeft,
    elemWid = _opts.elem.width(),
    elemHei = _opts.elem.height(),
    _fixed = _opts.fixed ? "fixed" : "absolute";
    //判断属性值是否是center的情况下,赋值不同
    // if(_opts.stick == "window" || _opts.stick == "document" || _opts.stick == "body"){       
    if (_opts.stick == "window") {
        _top = _opts.top == "center" ? ((windowHei - elemHei) * 0.5) : _opts.top;
        _left = _opts.left == "center" ? ((windowWid - elemWid) * 0.5) : _opts.left;
    } else {
        var _$stick = $(_opts.stick);
        if (_opts.top == "center") {
            _top = _$stick.offset().top + _$stick.height() * 0.5;
        } else {
            _top = _$stick.offset().top + _opts.top;
        }
        if (_opts.left == "center") {
            _left = _$stick.offset().left + _$stick.width() * 0.5;
        } else {
            _left = _$stick.offset().left + _opts.left;
        }
    }
    if ($.browser.msie && $.browser.version == "6.0") {
        //如果透明度为0,那么iframe的高宽和定位跟弹出框保持一致
        if (_opts.bgOpacity == 0) {
            $popupBoxBgIframe = $('<iframe id="popupBoxBgIframe" frameborder="0" scroll="no" style="position:absolute;z-Index:1002;filter:alpha(opacity:'+ _opts.bgOpacity * 100 +');width:' + elemWid + 'px;height:' + elemHei + 'px;"></iframe>').appendTo("body");
            //否则覆盖整个body
        } else {
            if ($("#popupBoxBg").length > 0) {
                $("#popupBoxBg").show();
            } else {
                $popupBoxBg = $('<div id="popupBoxBg" style="position:absolute;top:0;left:0;width:' + bodyWid + 'px;height:' + bodyHei + 'px;z-Index:1001;background:#000;filter:alpha(opacity:' + _opts.bgOpacity * 100 + ');"></div>').appendTo("body");
            }
            $popupBoxBgIframe = $('<iframe id="popupBoxBgIframe" frameborder="0" scroll="no" style="position:absolute;top:0;left:0;z-Index:1002;filter:alpha(opacity:'+ _opts.bgOpacity * 100 +');width:' + bodyWid + 'px;height:' + bodyHei + 'px;"></iframe>').appendTo("body");
        }
        //如果位置固定,不跟随滚动条滚动
        if (_opts.fixed) {
            _opts.elem[0].style.setExpression("top", "eval(document.documentElement.scrollTop +" + _top + ") + 'px'");
            _opts.elem[0].style.setExpression("left", "eval(document.documentElement.scrollLeft +" + _left + ") + 'px'");
            //位置不固定,跟随滚动条滚动
        } else {
            //如果透明度为0,那么iframe的定位跟弹出框保持一致
            if (_opts.bgOpacity == 0) {
                $("#popupBoxBgIframe").css({
                    top: _top,
                    left: _left
                })
            }
            _opts.elem.css({
                top: _top,
                left: _left
            })
        }
    } else {
        //如果背景透明度不是0,那么显示背景
        if (_opts.bgOpacity != 0) {
            if ($("#popupBoxBg").length > 0) {
                $("#popupBoxBg").show();
            } else {
                $popupBoxBg = $('<div id="popupBoxBg" style="position:fixed;top:0;left:0;width:100%;height:100%;z-Index:1001;background:#000;opacity:' + _opts.bgOpacity + ';"></div>').appendTo("body");
            }
        }
        _opts.elem.css({
            "position": _fixed,
            "zIndex": 1005,
            "top": _top,
            "left": _left
        });
    }
    _opts.showCallback();//弹出框显示后调用回调函数
    $window.on("resize", function () {
        var windowWid = $window.width(),
        windowHei = $window.height(),
        bodyWid = $body.width(),
        bodyHei = $body.height(),
        _scrollTop = document.documentElement.scrollTop,
        _scrollLeft = document.documentElement.scrollLeft;
        // if(_opts.bgOpacity != 0){
        //  $popupBoxBg.width(bodyWid).height(bodyHei);
        // }
        if ($.browser.msie && $.browser.version == "6.0") {
            if (_opts.bgOpacity != 0) {
                $popupBoxBgIframe.width(bodyWid).height(bodyHei);
            }
        }
        //如果相对元素是window,document,body
        // if(_opts.stick == "window" || _opts.stick == "document" || _opts.stick == "body"){
        if (_opts.stick == "window") {
            //只需要判断是否为"center",如果不是"center",resize时元素坐标不需要变
            if (_opts.top == "center") {
                //如果为ie6时
                if ($.browser.msie && $.browser.version == "6.0") {
                    //如果没有背,则$popupBoxBgIframe的位置要跟随$elem一起改变
                    if (_opts.bgOpacity == 0) {
                        //如果位置固定,不随滚动条滚动,就要+上documentElementScrollTop
                        if (_fixed) {
                            _opts.elem.add($popupBoxBgIframe).css({
                                top: _scrollTop + (windowHei - elemHei) * 0.5
                            })
                        } else {
                            _opts.elem.add($popupBoxBgIframe).css({
                                top: (windowHei - elemHei) * 0.5
                            })
                        }
                    } else {
                        if (_fixed) {
                            _opts.elem.css({
                                top: _scrollTop + (windowHei - elemHei) * 0.5
                            })
                        } else {
                            _opts.elem.css({
                                top: (windowHei - elemHei) * 0.5
                            })
                        }
                    }
                    //如果不是ie6
                } else {
                    _opts.elem.css({
                        top: (windowHei - elemHei) * 0.5
                    })
                }
            }
            //left值的情况同top
            if (_opts.left == "center") {
                if ($.browser.msie && $.browser.version == "6.0") {
                    if (_opts.bgOpacity == 0) {
                        if (_fixed) {
                            _opts.elem.add($popupBoxBgIframe).css({
                                left: _scrollLeft + (windowWid - elemWid) * 0.5
                            })
                        } else {
                            _opts.elem.add($popupBoxBgIframe).css({
                                left: (windowWid - elemWid) * 0.5
                            })
                        }
                    } else {
                        if (_fixed) {
                            _opts.elem.css({
                                left: _scrollLeft + (windowWid - elemWid) * 0.5
                            })
                        } else {
                            _opts.elem.css({
                                left: (windowWid - elemWid) * 0.5
                            })
                        }
                    }
                } else {
                    _opts.elem.css({
                        left: (windowWid - elemWid) * 0.5
                    })
                }
            }
            //如果相对元素不是window,document,body
            //这种情况就不用考虑scrollTop和Left了,只要考虑stick元素
        } else {
            if (_opts.top == "center") {
                _opts.elem.css({
                    top: _$stick.offset().top + _$stick.height() * 0.5
                })
                if ($.browser.msie && $.browser.version == "6.0" && _opts.bgOpacity == 0) {
                    $popupBoxBgIframe.css({
                        top: _$stick.offset().top + _$stick.height() * 0.5
                    })
                }
            } else {
                _opts.elem.css({
                    top: _$stick.offset().top + _opts.top
                })
                if ($.browser.msie && $.browser.version == "6.0" && _opts.bgOpacity == 0) {
                    $popupBoxBgIframe.css({
                        top: _$stick.offset().top + _opts.top
                    })
                }
            }
            if (_opts.left == "center") {
                _opts.elem.css({
                    left: _$stick.offset().left + _$stick.width() * 0.5
                })
                if ($.browser.msie && $.browser.version == "6.0" && _opts.bgOpacity == 0) {
                    $popupBoxBgIframe.css({
                        left: _$stick.offset().left + _$stick.width() * 0.5
                    })
                }
            } else {
                _opts.elem.css({
                    left: _$stick.offset().left + _opts.left
                })
                if ($.browser.msie && $.browser.version == "6.0" && _opts.bgOpacity == 0) {
                    $popupBoxBgIframe.css({
                        left: _$stick.offset().left + _opts.left
                    })
                }
            }
        }
    })
};
$.popupBox.setPosition.defaults = {
    elem: "",
    stick: "window",
    top: "center",
    left: "center",
    animate: "",
    fixed: true,
    bgOpacity: 0.5,
    showCallback: function () { }
}
//将popupBox添加为jQuery对一个的构造函数
$.fn.popupBox = function (_fnOpts) {
    var $this = $(this);
    _fnOpts = $.extend({}, $.popupBox.defaults, _fnOpts, {
        themeHtml: $this
    });
    $.popupBox(_fnOpts);
}
//将setPosition添加为jQuery对一个的构造函数
// $.fn.popupBox.setPosition = function (_fnPosOpts) {
//     var $this = $(this);
//     _fnPosOpts = $.extend({}, $.popupBox.setPosition.defaults, _fnPosOpts, {
//         elem: $this
//     });
//     $.popupBox.setPosition(_fnPosOpts);
// }
